package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.coupon.entity.BigWheel;
import com.wangsd.web.coupon.entity.BigWheelDetail;
import com.wangsd.web.coupon.entity.Company;
import com.wangsd.web.coupon.mapper.BigWheelDetailMapper;
import com.wangsd.web.coupon.mapper.BigWheelMapper;
import com.wangsd.web.coupon.service.BigWheelDetailService;
import com.wangsd.web.coupon.service.BigWheelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-22
 */
@Service
public class BigWheelServiceImpl extends ServiceImpl<BigWheelMapper, BigWheel> implements BigWheelService {

    @Resource
    BigWheelMapper bigWheelMapper;

    @Resource
    BigWheelDetailMapper bigWheelDetailMapper;

    @Autowired
    BigWheelDetailService bigWheelDetailService;

    @Override
    public IPage<BigWheel> selectPage(String code, Integer pageNum, Integer pageSize, BigWheel bigWheel) {
        Page<BigWheel> page = new Page<>(pageNum, pageSize);
        return bigWheelMapper.selectListXml(page, code, bigWheel);
    }

    @Override
    public BigWheel selectBigWheelByCompanyId(Long id) {
        QueryWrapper<BigWheel> query = new QueryWrapper<>();
        query.eq(" company_id", id);
        query.eq(" status", 1);
        BigWheel byQuery = bigWheelMapper.selectOne(query);
        return byQuery;
    }

    @Override
    public List<BigWheelDetail> selectBigWheelDetailByBigWheelId(Long id) {
        QueryWrapper<BigWheelDetail> query = new QueryWrapper<>();
        query.eq("big_wheel_id", id);
        List<BigWheelDetail> list = bigWheelDetailMapper.selectList(query);
        return list;
    }

    @Override
    public int addOrUpdate(BigWheel entity) {
        if (entity.getId() == null) {
            bigWheelMapper.insert(entity);
            /*保存大转盘和明细关联关系*/
            this.saveBigWheelDetail(entity.getBigWheelDetailList(), entity.getId());
        } else {
            /*先删除关联关系*/
            this.deleteDetailById(entity.getId());
            /*保存新的关联关系*/
            this.saveBigWheelDetail(entity.getBigWheelDetailList(), entity.getId());
            bigWheelMapper.updateById(entity);
        }
        return 0;
    }

    @Override
    public int updateStatus(BigWheel bigWheel) {
        /*当前对象上架，该商圈下的其他转盘下架*/
        if (bigWheel.getStatus() == 1) {
            BigWheel wheel = bigWheelMapper.selectById(bigWheel.getId());
            bigWheelMapper.updateStatusByCompanyId(wheel.getCompanyId(), 0);
        }
        int i = bigWheelMapper.updateById(bigWheel);
        return i;
    }

    private void saveBigWheelDetail(List<BigWheelDetail> details, Long bigWheelId) {
        if (details != null && details.size() > 0) {
            List<BigWheelDetail> list = new ArrayList<>();
            details.forEach(detail -> {
                BigWheelDetail bigWheelDetail = new BigWheelDetail();
                bigWheelDetail.setBigWheelId(bigWheelId);
                bigWheelDetail.setObjectId(detail.getObjectId());
                bigWheelDetail.setRate(detail.getRate());
                list.add(bigWheelDetail);
            });
            bigWheelDetailService.saveBatch(list);
        }
    }


    private void deleteDetailById(Long id) {
        QueryWrapper<BigWheelDetail> query = new QueryWrapper<>();
        query.eq("big_wheel_id", id);
        bigWheelDetailMapper.delete(query);
    }
}
